var em_number = null;
var em_name = null;
var em_totalmoney = null;
var em_maker = null;
var em_handlers = null;
var em_registrationtime = null;
var ot_audit = null;
var em_basemoney = null;
var em_mealmaoney = null;
var em_housingmoney = null;
var add = null;
var audit = null;
$(function() {
	em_number = $("#em_number");
	em_name = $("#em_name");
	em_totalmoney = $("#em_totalmoney");
	em_maker = $("#em_maker");
	em_handlers = $("#em_handlers");
	em_registrationtime = $("#em_registrationtime");
	ot_audit = $("#ot_audit");
	em_basemoney = $("#em_basemoney");
	em_mealmaoney = $("#em_mealmaoney");
	em_housingmoney = $("#em_housingmoney");
	add = $("#add");
	audit = $("#audit");
	//查询薪酬单账号是否存在
	$.ajax({
		type : "POST",
		url : "get_em_number.json",
		data : {
			em_number : em_number.val()
		},
		dataType : "json",
		success : function(data) {
			if (data.userCode == "exist") { // 账号已存在，由首页明细进入，将添加键隐藏
				add.css("display", "none"); 		
			} else { // 账号不存在，由首页添加进入，将审核键隐藏
				audit.css("display", "none"); 
			}
		}

	});
	function validateTip(element, placeholder, value, css, status) {
		element.attr("placeholder", value);
		element[0].style.border = css;
		element.attr("validateStatus", status);
	}

	// 判断薪酬单号是否存在

	em_name.bind('blur', function() {	
		if (em_name.val() ==0) {		
			validateTip(em_name, "placeholder", "请填写薪酬标准名称！", "1px solid red",
					false);
		} else {
			//查询用户选中的职位是否已经薪酬工资单
			$.ajax({
				type : "POST",
				url : "get_em_name.json",
				data : {
					em_name : em_name.val(),
					em_number:em_number.val()					
				},
				dataType : "json",
				success : function(data) {
					if (data.userCode == "exist") { // 职位已有对应的工资单，提示用户选择错误
						alert("该职位已有对应的工资单！")
						validateTip(em_name, "placeholder", "该职位已有对应的工资单！", "1px solid red",
								false); 
					} else { 
						validateTip(em_name, "", "", "1px solid #ccc", true);
					}
				}

			});
			
			
		}
	});

	ot_audit.bind('blur', function() {
		if (ot_audit.val() == null || ot_audit.val().length < 1) {
			validateTip(ot_audit, "placeholder", "请填写审核意见！", "1px solid red",
					false);
		} else {
			validateTip(ot_audit, "", "", "1px solid #ccc", true);
		}
	});

	em_basemoney.bind('blur', function() {
		if (em_basemoney.val() == null || em_basemoney.val().length < 1) {
			validateTip(em_basemoney, "placeholder", "请填写基本工资！",
					"1px solid red", false);
		} else {
			validateTip(em_basemoney, "", "", "1px solid #ccc", true);
		}
	});

	em_housingmoney.bind('blur', function() {
		if (em_housingmoney.val() == null || em_housingmoney.val().length < 1) {
			validateTip(em_housingmoney, "placeholder", "请填写住房补助！",
					"1px solid red", false);

		} else {
			validateTip(em_housingmoney, "", "", "1px solid #ccc", true);
		}
	});
	em_mealmaoney.bind('blur', function() {
		if (em_mealmaoney.val() == null || em_mealmaoney.val().length < 1) {
			validateTip(em_mealmaoney, "placeholder", "请填写餐补！",
					"1px solid red", false);
		} else {

			validateTip(em_mealmaoney, "", "", "1px solid #ccc", true);

		}
	});

	add.bind("click", function() {
	     if (em_name.attr("validateStatus") != "true") {
			em_name.blur();
		} else if (ot_audit.attr("validateStatus") != "true") {
			ot_audit.blur();
		} else if (em_basemoney.attr("validateStatus") != "true") {
			em_basemoney.blur();
		} else if (em_mealmaoney.attr("validateStatus") != "true") {
			em_mealmaoney.blur();
		} else if (em_housingmoney.attr("validateStatus") != "true") {
			em_housingmoney.blur();
		} else {
			if (confirm("是否确认提交数据")) {
				$("#form_demo").submit();
			}
		}
	});

	audit.bind("click", function() {
	 if (em_name.attr("validateStatus") != "true") {
			em_name.blur();
		} else if (ot_audit.attr("validateStatus") != "true") {
			ot_audit.blur();
		} else if (em_basemoney.attr("validateStatus") != "true") {
			em_basemoney.blur();
		} else if (em_mealmaoney.attr("validateStatus") != "true") {
			em_mealmaoney.blur();
		} else if (em_housingmoney.attr("validateStatus") != "true") {
			em_housingmoney.blur();
		} else {
			if (confirm("是否确认提交数据")) {
				$("#form_demo").submit();
			}
		}
	});
});

// 时间控件
!function() {
	laydate.skin('molv');// 切换皮肤，请查看skins下面皮肤库
	laydate({
		elem : '#em_registrationtime'
	});// 绑定元素
}();
// 自动计算薪酬总额
function enfocus() {
	document.forms[0].em_totalmoney.value = parseFloat(document.forms[0].em_basemoney.value)
			+ parseFloat(document.forms[0].em_housingmoney.value)
			+ parseFloat(document.forms[0].em_mealmaoney.value)
	setTimeout("enblur()", 500)
}
function enblur() {
	setTimeout("enfocus()", 5)
}